package com.hoppinzq.controller;

import com.alibaba.fastjson.JSON;
import com.hoppinzq.api.WyBuffApi;
import com.hoppinzq.params.CodeQr;
import com.hoppinzq.service.bean.ApiResponse;
import com.hoppinzq.service.http.HoppinHttpClient;
import com.hoppinzq.service.http.HttpApiExecutorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author ZhangQi
 */
@RestController
public class WyBuffController {

    @Autowired
    private HttpServletResponse httpServletResponse;

    @RequestMapping("/test1")
    public String test() throws InterruptedException {
        String now=String.valueOf(System.currentTimeMillis());
        return String.valueOf(System.currentTimeMillis());
    }

    @RequestMapping("/test2")
    public String test2() throws InterruptedException {
        String now=String.valueOf(System.currentTimeMillis());
        return String.valueOf(System.currentTimeMillis());
    }

    @RequestMapping("/createQr")
    public ApiResponse createQr(){
        HoppinHttpClient hoppinHttpClient = new HoppinHttpClient
                .BuilderClient()
                .encode("GBK")
                .charset("UTF-8")
                .build();
        WyBuffApi api = HttpApiExecutorFactory.create(WyBuffApi.class,hoppinHttpClient);
        CodeQr qr=new CodeQr(1,new HashMap(0));
        String json= JSON.toJSONString(qr);
        String response=api.createQr(json);
        Map<String, List<String>> responseHeader = hoppinHttpClient.getResponseHeader();
        for(String key:responseHeader.keySet()){
            System.err.println(key+":"+responseHeader.get(key));
        }
        if(hoppinHttpClient.isSuccess()){
            return ApiResponse.success(response);
        }else{
            return ApiResponse.fail(hoppinHttpClient.getCode(),response);
        }
    }

    @RequestMapping("/getQrState")
    public ApiResponse getQrState(String code_id){
        HoppinHttpClient hoppinHttpClient = new HoppinHttpClient
                .BuilderClient()
                .encode("GBK")
                .charset("UTF-8")
                .build();
        WyBuffApi api = HttpApiExecutorFactory.create(WyBuffApi.class,hoppinHttpClient);
        String response=api.getQrState(code_id);
        Map<String, List<String>> responseHeader = hoppinHttpClient.getResponseHeader();
        for(String key:responseHeader.keySet()){
            System.err.println(key+":"+responseHeader.get(key));
//            if(key!=null){
//                List<String> values=responseHeader.get(key);
//                for(String value:values){
//                    httpServletResponse.setHeader(key,value);
//                }
//            }
        }
        if(hoppinHttpClient.isSuccess()){
            return ApiResponse.success(response);
        }else{
            return ApiResponse.fail(hoppinHttpClient.getCode(),response);
        }
    }

    @RequestMapping("/buffQrLogin")
    public ApiResponse buffQrLogin(String code_id){
        HoppinHttpClient hoppinHttpClient = new HoppinHttpClient
                .BuilderClient()
                .encode("GBK")
                .charset("UTF-8")
                .build();
        WyBuffApi api = HttpApiExecutorFactory.create(WyBuffApi.class,hoppinHttpClient);
        Map params=new HashMap();
        params.put("item_id",code_id);
        String cookie="";
        String response=api.buffQrLogin(JSON.toJSONString(params),cookie);
        if(hoppinHttpClient.isSuccess()){
            return ApiResponse.success(response);
        }else{
            return ApiResponse.fail(hoppinHttpClient.getCode(),response);
        }
    }

    @RequestMapping("/file")
    public String getFileName(MultipartFile file){
        return file.getName();
    }

    public static void main(String[] args) {
        long start =System.currentTimeMillis();

        ExecutorService executor = Executors.newFixedThreadPool(5);

        for (int i = 0; i < 100; i++) {
            // 提交任务到线程池
            final int index = i;
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    HoppinHttpClient hoppinHttpClient = new HoppinHttpClient
                            .BuilderClient()
                            .encode("GBK")
                            .charset("UTF-8")
                            .build();
                    WyBuffApi api = HttpApiExecutorFactory.create(WyBuffApi.class,hoppinHttpClient);
                    String cookie="ldapinfo=false; Hm_lvt_fe6ce4a74013f66d1a1715ab1226564e=1694846705,1696400743; _sd2j=%7B%22data%22%3A%7B%22userName%22%3A%22JUMyJTgzQ3AlN0NoNHQlMTRseXVQbCUzRjN4OG0%3D%22%2C%22password%22%3A%22JTNGZ0ZFMiU3QmUlMDBSRUpFViUxN04lMEVuJTVFdGR0IXNnayUyM29V%22%7D%7D; SID=56d9da23858c4356ae208115f742991f!20231108083847; BBCloudBAMSession=AQIC5wM2LY4SfczF7BDOrKp_B0l0HIIDE3Lke48k4PX7scY.*AAJTSQACMDIAAlNLABQtMTE2MzE1ODk4NTU1NzU1OTg4MAACUzEAAjAx*";
                    Map params=new HashMap();
                    params.put("postsId","231108165701738046");
                    api.blog(JSON.toJSONString(params),cookie);
                }
            });
        }
        executor.shutdown();
        while (!executor.isTerminated()) {

        }
        long end=System.currentTimeMillis();
        System.err.println("持续时间："+(end-start)/1000+" 秒");
    }

}

